package en.kaist.cs408.acav;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
import en.kaist.cs408.R;
import en.kaist.cs408.acav.CreateClass.NetworkHandler;

public class RealTimeAttendanceList extends Activity {

	String courseID;
	String pwdEncrypt;
	
	Button stopChecking;
	Button refresh;
	
	MyAdapter mAdapter;
	ListView studentList;
	int mPosition;
	AlertDialog alertDialog;
	AlertDialog ad;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.realtimeattendancelist);

		Intent intent = getIntent();
		courseID = intent.getExtras().getString("CourseID");
		pwdEncrypt = intent.getExtras().getString("PwdEncrypt");
		
		stopChecking = (Button) findViewById(R.id.buttonStop);
		refresh = (Button) findViewById(R.id.buttonRefresh);
		
		stopChecking.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				/*
				 * Really stop the attendance checking? (AlertDialog) if yes,
				 * signal to server to stop attendance checking (set null to
				 * hotspot address in course table by the course ID)
				 * 
				 * 
				 * 
				 * if no, null
				 */
				AlertDialog.Builder ad = new AlertDialog.Builder(RealTimeAttendanceList.this);
				ad.setTitle("Alert");
				ad.setMessage("Do you really want to STOP attendance checking?");
				ad.setNegativeButton("No", null);
				
				ad.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						NetworkHandler handler = new NetworkHandler(){
							public void handleMessage(Message msg){
								switch (msg.what){
								case 0:
									stopChecking.setEnabled(false);
									refresh.setEnabled(false);
									Toast.makeText(RealTimeAttendanceList.this, "Server Complete", Toast.LENGTH_LONG).show();
									break;
								default:
									Toast.makeText(RealTimeAttendanceList.this, "Exception Occurred", Toast.LENGTH_LONG).show();
									break;	
								}
							}
						};
						
						NetworkThread nt = new NetworkThread(2100, courseID, null, null, null, null,null,null, false, handler);
						nt.start();
						
						WifiApManager wifiApManager = new WifiApManager(RealTimeAttendanceList.this);
						if(wifiApManager.getWifiApState().equals(WIFI_AP_STATE.WIFI_AP_STATE_ENABLED)){
							wifiApManager.setWifiApEnabled(null, false);
						}
					}
				});
				
				ad.show();
			}
		});

		
		mAdapter = new MyAdapter(new ArrayList<Student>(), this);
		studentList = (ListView) findViewById(R.id.studentListView);
		
		studentList.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				mPosition = position;
				String studId = mAdapter.getItem(mPosition).getId();
				initAlertDialog(studId);
				return false;
			}

		});

		studentList.setAdapter(mAdapter);

		
		
		refresh.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				NetworkHandler handler = new NetworkHandler(){
					public void handleMessage(Message msg){
						switch (msg.what){
						case 0:
							if (mAdapter.getCount() != 0)
								Toast.makeText(RealTimeAttendanceList.this, "List UP!!", Toast.LENGTH_LONG).show();
							else
								Toast.makeText(RealTimeAttendanceList.this, "No result found on this date", Toast.LENGTH_LONG).show();
							mAdapter.notifyDataSetChanged();
							break;
						case -1:
							Toast.makeText(RealTimeAttendanceList.this, "Wrong Course ID or Password", Toast.LENGTH_LONG).show();
							break;
						default:
							Toast.makeText(RealTimeAttendanceList.this, "Exception Occurred", Toast.LENGTH_LONG).show();
							break;	
						}
					}
				};
				
				SimpleDateFormat today;
				long todayTime = System.currentTimeMillis();
				today = new SimpleDateFormat("yyyy-MM-dd");
				String todayStr = today.format(new Date(todayTime));
				
				mAdapter.clear();
				
				NetworkThread nt = new NetworkThread(4000, courseID, null, pwdEncrypt, null, null, todayStr, mAdapter, false, handler);
				nt.start();
			}
		});
	}

	private void initAlertDialog(String studentID) {
		alertDialog = new AlertDialog.Builder(this)
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle("Check attendance manually")
				.setMessage(
						"Do you want to check attendance manually for student: "
								+ "\n" + "\"" + studentID + "\"" + " ?")
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						// mPosition = -1;
					}
				})
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								// db.supprimerUnePhrase(nomDuPatient,
								// mlistAdapt.getItem(mPosition));
								// mlistAdapt.remove(mlistAdapt.getItem(mPosition));
								// mlistAdapt.notifyDataSetChanged();
								// mPosition = -1;

							}
						}).create();
		alertDialog.show();
	}
}
